package com.teach.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitmqConfig {

    // 延时交换机名称
    public static final String DELAY_EXCHANGE = "delayone";
    // 订单延时队列名称
    public static final String DELAY_ORDER_QUEUE = "orderone";
    // 死信订单routingkey
    public static final String DELAY_MESSAGE_KEY = "delayMessageKeyone";
    /**
     * 延时队列交换机配置和使用
     * @return
     */
    @Bean("delayedExchange")
    public CustomExchange delayedExchange(){
        /**
         * 1、delayedExchange 交换机名称
         * 2、x-delayed-message  交换机类型
         * 3、true 是否持久化
         * 4、false 是否自动删除
         * 5、arguments 设置交换机类型
         */
        Map<String, Object> arguments = new HashMap<String, Object>();
        arguments.put("x-delayed-type","direct");
        return new CustomExchange(DELAY_EXCHANGE,
                "x-delayed-message",true,false,arguments);
    }

    /**
     * 订单的队列的设置
     * @return
     */
    @Bean("orderQueue")
    public Queue orderQueue(){
        return new Queue(DELAY_ORDER_QUEUE);
    }
    /**
     * 延时队列的配置
     * @param orderQueue
     * @param delayedExchange
     * @return
     */
    @Bean
    public Binding delayedBingMessageExchange(@Qualifier("orderQueue")Queue orderQueue,
                                              @Qualifier("delayedExchange")CustomExchange delayedExchange){
        return BindingBuilder.bind(orderQueue).to(delayedExchange)
                .with(DELAY_MESSAGE_KEY).noargs();
    }

}